package com.sena.crawler.model;

import com.sena.crawler.util.JedisUtilNew;
import com.sena.crawler.util.RegexUtil;
import com.sena.crawler.util.UrlUtil;
import com.sena.crawler.conf.SenaCrawlerConf;
import com.sena.crawler.loader.PageLoader;
import com.sena.crawler.parser.PageParser;
import com.sena.crawler.loader.strategy.JsoupPageLoader;
import com.sena.crawler.proxy.ProxyMaker;

import java.util.*;

/**
 * site info
 *
 * @author seven-12-14 15:52:49
 */
public class RunConf {

    private volatile boolean allowSpread = true;                                    // 允许扩散爬取，将会以现有URL为起点扩散爬取整站
    private Set<String> whiteUrlRegexs = Collections.synchronizedSet(new HashSet<String>());    // URL白名单正则，非空时进行URL白名单过滤页面
    private Set<String> blackUrlRegexs = Collections.synchronizedSet(new HashSet<String>());    // URL黑名单正则，非空时进行URL白名单过滤页面
    private PageLoader pageLoader = new JsoupPageLoader();                          // 页面下载器

    private volatile Map<String, String> paramMap;                                  // 请求参数
    private volatile Map<String, String> cookieMap;                                 // 请求Cookie
    private volatile Map<String, String> headerMap;                                 // 请求Header
    private volatile List<String> userAgentList = Collections.synchronizedList(new ArrayList<String>(Arrays.asList(SenaCrawlerConf.USER_AGENT_CHROME)));     // 请求UserAgent
    private volatile String referrer;                                               // 请求Referrer
    private volatile boolean ifPost = false;                                        // 请求方式：true=POST请求、false=GET请求
    private volatile int timeoutMillis = SenaCrawlerConf.TIMEOUT_MILLIS_DEFAULT;     // 超时时间，毫秒
    private volatile int pauseMillis = 0;                                           // 停顿时间，爬虫线程处理完页面之后进行主动停顿，避免过于频繁被拦截；
    private volatile ProxyMaker proxyMaker;                                         // 代理生成器
    private volatile int failRetryCount = 0;                                        // 失败重试次数，大于零时生效
    private volatile boolean isValidateTLSCertificates = true;                      // 是否验证https

    private volatile Set<String> keyword = Collections.synchronizedSet(new HashSet<String>());//关键字
    private volatile boolean allowRegex = false;

    private volatile String senaCrawlerType;

    private volatile Set<String> suffixs = Collections.synchronizedSet(new HashSet<String>());

    private volatile String jdbcUrl;//数据库连接url

    private volatile String jdbcUserName;//数据库连接用户名

    private volatile String jdbcUserPassWord;//数据库连接用户名密码

    private volatile JedisUtilNew jedisUtil;

    private volatile String rootLink;
    /**
     * 二级域名的个数
     */
    private volatile Integer maxUrl=20000;

    private volatile boolean needMax = false;




    // util

    /**
     * valid url, include white url
     *
     * @param link
     * @return boolean
     */
    public boolean validWhiteUrl(String link) {
        if (!UrlUtil.isUrl(link)) {
            return false;
        }
        if (whiteUrlRegexs != null && whiteUrlRegexs.size() > 0) {
            boolean underWhiteUrl = false;
            for (String whiteRegex : whiteUrlRegexs) {
                if (RegexUtil.matches(whiteRegex, link)) {
                    underWhiteUrl = true;
                }
            }
            if (!underWhiteUrl) {
                return false;   // check white
            }
        }
        return true;    // true if regex is empty
    }

    /**
     * 黑名单校验
     *
     * @param link
     * @return
     */
    public boolean validBlackUrl(String link) {
        if (!UrlUtil.isUrl(link)) {
            return true;
        }
        if (blackUrlRegexs != null && blackUrlRegexs.size() > 0) {
            boolean underWhiteUrl = false;
            for (String whiteRegex : blackUrlRegexs) {
                if (RegexUtil.matches(whiteRegex, link)) {
                    return true;
                }
            }
            if (!underWhiteUrl) {
                return false;   // check black
            }
        }
        return false;    // true if regex is empty
    }

    public String getSenaCrawlerType() {
        return senaCrawlerType;
    }

    public void setSenaCrawlerType(String senaCrawlerType) {
        this.senaCrawlerType = senaCrawlerType;
    }

    public boolean isAllowRegex() {
        return allowRegex;
    }

    public void setAllowRegex(boolean allowRegex) {
        this.allowRegex = allowRegex;
    }

    public boolean isAllowSpread() {
        return allowSpread;
    }

    public void setAllowSpread(boolean allowSpread) {
        this.allowSpread = allowSpread;
    }

    public Set<String> getWhiteUrlRegexs() {
        return whiteUrlRegexs;
    }

    public void setWhiteUrlRegexs(Set<String> whiteUrlRegexs) {
        this.whiteUrlRegexs = whiteUrlRegexs;
    }

    public PageLoader getPageLoader() {
        return pageLoader;
    }

    public void setPageLoader(PageLoader pageLoader) {
        this.pageLoader = pageLoader;
    }

    public Map<String, String> getParamMap() {
        return paramMap;
    }

    public void setParamMap(Map<String, String> paramMap) {
        this.paramMap = paramMap;
    }

    public Map<String, String> getCookieMap() {
        return cookieMap;
    }

    public void setCookieMap(Map<String, String> cookieMap) {
        this.cookieMap = cookieMap;
    }

    public Map<String, String> getHeaderMap() {
        return headerMap;
    }

    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    public List<String> getUserAgentList() {
        return userAgentList;
    }

    public void setUserAgentList(List<String> userAgentList) {
        this.userAgentList = userAgentList;
    }

    public String getReferrer() {
        return referrer;
    }

    public void setReferrer(String referrer) {
        this.referrer = referrer;
    }

    public boolean isIfPost() {
        return ifPost;
    }

    public void setIfPost(boolean ifPost) {
        this.ifPost = ifPost;
    }

    public int getTimeoutMillis() {
        return timeoutMillis;
    }

    public void setTimeoutMillis(int timeoutMillis) {
        this.timeoutMillis = timeoutMillis;
    }

    public int getPauseMillis() {
        return pauseMillis;
    }

    public void setPauseMillis(int pauseMillis) {
        this.pauseMillis = pauseMillis;
    }

    public ProxyMaker getProxyMaker() {
        return proxyMaker;
    }

    public void setProxyMaker(ProxyMaker proxyMaker) {
        this.proxyMaker = proxyMaker;
    }

    public int getFailRetryCount() {
        return failRetryCount;
    }

    public void setFailRetryCount(int failRetryCount) {
        this.failRetryCount = failRetryCount;
    }

    public boolean isValidateTLSCertificates() {
        return isValidateTLSCertificates;
    }

    public void setValidateTLSCertificates(boolean isValidateTLSCertificates) {
        this.isValidateTLSCertificates = isValidateTLSCertificates;
    }

    public Set<String> getBlackUrlRegexs() {
        return blackUrlRegexs;
    }

    public void setBlackUrlRegexs(Set<String> blackUrlRegexs) {
        this.blackUrlRegexs = blackUrlRegexs;
    }


    public Set<String> getKeyword() {
        return keyword;
    }

    public void setKeyword(Set<String> keyword) {
        this.keyword = keyword;
    }

    public Set<String> getSuffixs() {
        return suffixs;
    }

    public void setSuffixs(Set<String> suffixs) {
        this.suffixs = suffixs;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getJdbcUserName() {
        return jdbcUserName;
    }

    public void setJdbcUserName(String jdbcUserName) {
        this.jdbcUserName = jdbcUserName;
    }

    public String getJdbcUserPassWord() {
        return jdbcUserPassWord;
    }

    public void setJdbcUserPassWord(String jdbcUserPassWord) {
        this.jdbcUserPassWord = jdbcUserPassWord;
    }

    public JedisUtilNew getJedisUtil() {
        return jedisUtil;
    }

    public void setJedisUtil(JedisUtilNew jedisUtil) {
        this.jedisUtil = jedisUtil;
    }

    public Integer getMaxUrl() {
        return maxUrl;
    }

    public void setMaxUrl(Integer maxUrl) {
        this.maxUrl = maxUrl;
    }

    public boolean isNeedMax() {
        return needMax;
    }

    public void setNeedMax(boolean needMax) {
        this.needMax = needMax;
    }

    public String getRootLink() {
        return rootLink;
    }

    public void setRootLink(String rootLink) {
        this.rootLink = rootLink;
    }
}
